<?php
/**
 * Created by PhpStorm.
 * User: Menger
 * Date: 2020/12/09
 * Time: 12:00
 * Desc: 系统 - 轮播图
 */

namespace app\admin\controller;

use think\Request;
use app\common\model\Banner as BannerModel;

class Banner extends Base
{
    /**
     * index
     * @author Meng
     * @dateTime 2020-12-09
     * @param    Request    $request [description]
     * @return   [type]              [description]
     */
    public function index(Request $request)
    {
        if ($request->keyword) {
            $where_user[] = ['name', 'like', '%' . $request->keyword . '%'];
        }

        $this->_search_filter($where_user);

        $where_user[] = ['is_del', 'eq', 0];

        $banner_model = new BannerModel();
        $banner_obj = $this->_list($banner_model, $where_user,['sort'=>'desc','id'=>'desc']);

        $this->assign('banner_obj', $banner_obj);

        if ($request->isAjax()) {
            $banner_obj['data']->append();
            return $banner_obj;
        }
        return $this->fetch();
    }

    /**
     * add
     * @author Meng
     * @dateTime 2020-12-09
     * @param    Request    $request [description]
     * @return   [type]              [description]
     */
    public function add()
    {
        return $this->fetch();
    }

    /**
     * edit
     * @author Meng
     * @dateTime 2020-12-09
     * @param    Request    $request [description]
     * @return   [type]              [description]
     */
    public function edit(Request $request)
    {
        $vo = $this->_edit($request);
        return $this->fetch();
    }

    /**
     * save
     * @author Meng
     * @dateTime 2020-12-09
     * @param    Request    $request [description]
     * @return   [type]              [description]
     */
    public function save(Request $request)
    {
        $controller = $request->controller();
        switch ($request->opmode) {
            case "add" :
                $result = $this->_insert($request, $controller);
                $msg = '新增成功';
                break;
            case "edit" :
                $result = $this->_update($request, $controller);
                $msg = '编辑成功';
                break;
            default :
                $msg = '非法操作';
                $result = false;
        }
        if ($request->isAjax()) {
            if ($result) {
                return $this->api_success($msg, $result);
            }
            return $this->api_error($msg, $result);
        }
    }

    /**
     * del
     * @author Meng
     * @dateTime 2020-12-09
     * @param    Request    $request [description]
     * @return   [type]              [description]
     */
    public function del(Request $request)
    {
        $result = $this->_del($request);
        if ($result) {
            return $this->api_success('删除成功');
        }
        return $this->api_error('删除失败');
    }

    /**
     * set_valid
     * @author Meng
     * @dateTime 2020-12-09
     * @param    Request    $request [description]
     */
    public function set_valid(Request $request)
    {
        return $this->_set_valid($request);
    }

    /**
     * set_valid
     * @author Meng
     * @dateTime 2020-12-09
     * @param    Request    $request [description]
     */
    public function set_invalid(Request $request)
    {
        return $this->_set_invalid($request);
    }

    /**
     * upload
     * @author Meng
     * @dateTime 2020-12-09
     * @param    Request    $request [description]
     * @return   [type]              [description]
     */
    public function upload()
    {
        return $this->_upload();
    }
    
}